package com.ly.cloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.common.mybatisplus.plugins.PageInfo;
import com.ly.cloud.dto.ApprovalDetailDto;
import com.ly.cloud.service.ApprovalService;
import com.ly.cloud.vo.ApprovalCategoryVo;
import com.ly.cloud.vo.ApprovalDetailVo;
import com.ly.cloud.vo.RoleVo;
import com.ly.cloud.web.utils.WebResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 流程管理 通用审批方法
 * 
 * @author siqi
 * @date: 2018年12月7日
 */
@RestController
@RequestMapping("/apvl")
@Api("本控制器主要控制审批流程的是否启用及审批流程步骤的设置")
public class ApprovalController {

	@Autowired
	public ApprovalService approvalService;

	/**
	 * 查询审批流程
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param name
	 * @return
	 */
	@ApiOperation(value = "分页查询审批流程类别", notes = "分页查询审批流程类别")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "int", required = true, value = "当前页码", defaultValue = "1"),
			@ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", required = true, value = "每页条数", defaultValue = "5"),
			@ApiImplicitParam(paramType = "query", name = "name", dataType = "String", required = false, value = "流程名称", defaultValue = "") })
	@RequestMapping(value = "/queryAllApprovalCategory", method = RequestMethod.GET)
	public WebResponse<PageInfo<ApprovalCategoryVo>> queryAllApprovalCategory(@RequestParam int pageNum,
			@RequestParam int pageSize, @RequestParam(required = false) String name) {
		try {
			Page<ApprovalCategoryVo> page = approvalService
					.queryPageAprvCategory(new Page<ApprovalCategoryVo>(pageNum, pageSize), name);
			return new WebResponse<PageInfo<ApprovalCategoryVo>>().success(PageInfo.toPageInfo(page));
		} catch (Exception e) {
			e.printStackTrace();
			return new WebResponse<PageInfo<ApprovalCategoryVo>>().failure(e.getMessage());
		}
	}

	/**
	 * 根据审批流程ID查询对应的审批步骤
	 * 
	 * @param categoryID
	 *            审批流程ID
	 * @return
	 */
	@RequestMapping(value = "/queryApprovalDetailByCategoryID", method = RequestMethod.GET)
	public WebResponse<List<ApprovalDetailVo>> queryApprovalDetailByCategoryID(@RequestParam Long categoryID) {
		try {
			List<ApprovalDetailVo> detailList = approvalService.queryApprovalDetailByCategoryID(categoryID);
			return new WebResponse<List<ApprovalDetailVo>>().success(detailList);
		} catch (Exception e) {
			e.printStackTrace();
			return new WebResponse<List<ApprovalDetailVo>>().failure(e.getMessage());
		}
	}

	/**
	 * 设置审批流程是否启用
	 * 
	 * @param is_enable
	 *            当前是否启用状态
	 * @param categoryID
	 *            审批流程ID
	 * @return
	 */
	@RequestMapping(value = "/updateCategoryIsEnable", method = RequestMethod.GET)
	public WebResponse<Boolean> updateCategoryIsEnable(@RequestParam("is_enable") Long is_enable,
			@RequestParam("categoryID") Long categoryID) {
		try {
			boolean enable = approvalService.updateCategoryIsEnable(is_enable, categoryID);
			return new WebResponse<Boolean>().success(enable);
		} catch (Exception e) {
			e.printStackTrace();
			return new WebResponse<Boolean>().failure(e.getMessage());
		}
	}

	/**
	 * 新增审批步骤
	 * 
	 * @param dto
	 * @return
	 */
	@RequestMapping(value = "/insertApprovalDetail", method = RequestMethod.POST)
	public WebResponse<Boolean> insertApprovalDetail(
			@RequestHeader(required = false, value = "loginUserName") String loginUserName,
			@RequestBody ApprovalDetailDto dto) {
		try {
			boolean rs = approvalService.insertApprovalDetail(loginUserName, dto);
			return new WebResponse<Boolean>().success(rs);
		} catch (Exception e) {
			return new WebResponse<Boolean>().failure(e.getMessage());
		}
	}

	/**
	 * 设置审批步骤是否启用
	 * 
	 * @param is_enable
	 * @param detailID
	 * @return
	 */
	@RequestMapping(value = "/updateDetailIsEnable", method = RequestMethod.GET)
	public WebResponse<Boolean> updateDetailIsEnable(@RequestParam("is_enable") Long is_enable,
			@RequestParam("detailID") Long detailID) {
		try {
			boolean enable = approvalService.updateDetailIsEnable(is_enable, detailID);
			return new WebResponse<Boolean>().success(enable);
		} catch (Exception e) {
			e.printStackTrace();
			return new WebResponse<Boolean>().failure(e.getMessage());
		}
	}

	/**
	 * 查询角色列表
	 * 
	 */
	@RequestMapping(value = "/querRolesList", method = RequestMethod.GET)
	public WebResponse<List> queryRolesList() {
		List<RoleVo> roleList = approvalService.queryRolesList();
		return new WebResponse<List>().success(roleList);
	}

	/**
	 * 修改审批流程步骤
	 */
	@RequestMapping(value = "/updateDetailById", method = RequestMethod.POST)
	public WebResponse<Boolean> updateDetailById(
			@RequestHeader(required = false, value = "loginUserName") String loginUserName,
			@RequestBody ApprovalDetailDto dto) {
		boolean isUpdate = approvalService.updateDetailById(loginUserName, dto);
		return new WebResponse<Boolean>().success(isUpdate);
	}

	// ===============================================审批操作=========================================
	/**
	 * 审批初始化，提交业务时调用
	 * 
	 * @param ywzj
	 *            业务主键
	 * @param tableName
	 *            对应的业务表名，例如LY_CKBZ_GXJW_KCSJSQL
	 * @return 返回的信息(0:成功,1:未定义流程,2:不启用审批流程,直接通过)
	 * @author caihuihai
	 * @date 2018-12-25
	 */
	@RequestMapping(value = "/initApproval", method = RequestMethod.POST)
	public WebResponse<String> initApproval(@RequestParam(value = "ywzj") String ywzj,
			@RequestParam(value = "tableName") String tableName) {
		String msg = approvalService.initApproval(ywzj, tableName);
		return new WebResponse<String>().success(msg);
	}

	/**
	 * 审批处理过程
	 * 在获取返回值的时候后需在代码中判断此值是否为null,如何不为null则要更新审批状态为此返回值.
	 * @param ywzj
	 *            业务主键
	 * @param tableName
	 *            对应的业务表名，例如LY_CKBZ_GXJW_KCSJSQL
	 * @param sprbh
	 *            审批人工号
	 * @param spyj
	 *            审批意见
	 * @param spzt
	 *            审批状态(1:通过 , 0:不通过)
	 * @return 返回的审批状态信息(8:审批不通过,9审批通过)
	 * @author caihuihai
	 * @date 2018-12-25
	 */
	@RequestMapping(value = "/processApproval", method = RequestMethod.POST)
	public WebResponse<String> processApproval(@RequestParam(value = "ywzj") String ywzj,
			@RequestParam(value = "tableName") String tableName, @RequestParam(value = "sprbh") String sprbh,
			@RequestParam(value = "spyj", required = false) String spyj, @RequestParam(value = "spzt") String spzt) {
		String msg = approvalService.processApproval(ywzj, tableName, sprbh, spyj, spzt);
		return new WebResponse<String>().success(msg);
	}
	
	/**
     * 根据表编号查询编码规则，并返回按规则生成的对应值
     * @param bbh 表编号
     * @return
     */
    @RequestMapping(value = "/genRuleCode", method = RequestMethod.GET)
    public WebResponse<String> genRuleCode(@RequestParam String bbh){
        String code = "123456"+bbh;
        return new WebResponse<String>().success(code);
    }

}
